package com.wh.wisdomsite.xbox.server.controller.vi;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.pojo.vi.VideoDevDataIn;
import com.wh.wisdomsite.xbox.pojo.vi.VideoProjectIn;
import com.wh.wisdomsite.xbox.server.util.DateUtils;
import com.wh.wisdomsite.xbox.server.util.UserUtils;
import com.wh.wisdomsite.xbox.server.util.WrapperUtil;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.vi.entity.VideoProject;
import com.wh.wisdomsite.xbox.vi.service.IVideoProjectService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 视频监控设备列表
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.vi
 * @date 2018年09月03日 18:11
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/videoDevData")
public class VideoDevDataController {
    private static final Log logger = LogFactory.getLog(VideoDevDataController.class);

    @Autowired
    private IVideoProjectService iVideoProjectService;

    /**
     * 跳转到视频监控实时数据
     *
     * @return
     */
    @RequiresPermissions("rules:vi_videoDevData_page")
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index() {
        return "vi/videoDevData/videoDevDataList";
    }


    /**
     * 获取设备对应的项目数据列表
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:vi_videoDevData_find")
    @RequestMapping(value = "/videoDevDataList.do")
    public void videoDevDataList(HttpServletResponse response, VideoDevDataIn in) {
        try {
            logger.info("加载设备对应的项目数据列表-->>开始");
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            Page<VideoProject> page = new Page<VideoProject>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            EntityWrapper<VideoProject> ewVideoProjectPrj = new EntityWrapper<VideoProject>();
            ewVideoProjectPrj.setEntity(new VideoProject());
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewVideoProjectPrj.eq("pr.prId",in.getPrId());
            }
            if(StringUtils.isNotEmpty(in.getOnLineStatus())){
                ewVideoProjectPrj.eq("v.onLineStatus",in.getOnLineStatus());
            }
            //若用户不是超级管理员根据用户的数据权限添加查询条件
            if(!UserUtils.isSysAdmin()){
                ewVideoProjectPrj = UserUtils.setEntityWrapper(ewVideoProjectPrj, "co", "off", "pr");
            }
            ewVideoProjectPrj.eq("v.dataState",1);
            ewVideoProjectPrj.groupBy("pr.prId");
            Page<Map<String, Object>> videoPrjPage = iVideoProjectService.getPrjMapsPage(page,ewVideoProjectPrj);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if (videoPrjPage.getRecords().size() > 0) {
                for(Map<String, Object> videpPrj : videoPrjPage.getRecords()){
                    JSONObject object = new JSONObject(videpPrj);
                    object.put("prId",videpPrj.get("prId"));
                    object.put("prName",videpPrj.get("prName"));
                    object.put("pbUnitName",videpPrj.get("pbUnitName"));
                    object.put("poUnitName",videpPrj.get("poUnitName"));
                    object.put("psUnitName",videpPrj.get("psUnitName"));
                    object.put("userUuid",videpPrj.get("userUuid"));
                    object.put("videoUrl",videpPrj.get("videoUrl"));
                    object.put("hkUserName",videpPrj.get("hkUserName"));
                    array.put(object);
                }
            }
            json.put("total", videoPrjPage.getTotal());
            json.put("rows", array);
            ResManager.getJsonOut(response).print(json);
            logger.info("加载设备对应的项目数据列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转设备对应的项目数据列表
     * @param request
     * @param in
     */
    @RequestMapping(value = "/toVideoDeviceList.do")
    public String toVideoDeviceList(HttpServletRequest request, VideoDevDataIn in) {
        String projectId = in.getPrId();
        String onLineStatus = in.getOnLineStatus();
        request.setAttribute("projectId",projectId);
        request.setAttribute("onLineStatus",onLineStatus);
        return "vi/videoDevData/videoDeviceList";
    }

    /**
     * 跳转视频设备播放页面
     * @param request
     * @param userUuid
     * @param videoUrl
     * @return
     */
    @RequiresPermissions("rules:vi_videoDevData_viewBtn")
    @RequestMapping(value = "/toVideoUrl.do")
    public String toVideoUrl(HttpServletRequest request, String userUuid, String videoUrl) {
        if(StringUtils.isEmpty(userUuid)){
            userUuid = "";
        }
        if(StringUtils.isEmpty(videoUrl)){
            videoUrl = "";
        }
        request.setAttribute("userUuid",userUuid);
        request.setAttribute("videoUrl",videoUrl);
        return "vi/videoDevData/videoViews";
    }

    /**
     * 根据项目ID跳转到视频播放页面
     * @param prId
     * @return
     */
    @RequiresPermissions("rules:vi_videoDevData_openByPrId")
    @RequestMapping( value = "/openByPrId.do")
    public String openByPrId(String prId){
        Page<VideoProject> page = new Page<>();
        page.setCurrent(1);
        page.setSize(1);
        //设置条件
        EntityWrapper<VideoProject> ewVideoProjectPrj = new EntityWrapper<VideoProject>();
        ewVideoProjectPrj.setEntity(new VideoProject());
        if(StringUtils.isNotEmpty(prId)){
            ewVideoProjectPrj.eq("pr.prId",prId);
        }
        //若用户不是超级管理员根据用户的数据权限添加查询条件
        if(!UserUtils.isSysAdmin()){
            ewVideoProjectPrj = UserUtils.setEntityWrapper(ewVideoProjectPrj, "co", "off", "pr");
        }
        ewVideoProjectPrj.eq("v.dataState",1);
        ewVideoProjectPrj.groupBy("pr.prId");
        Page<Map<String, Object>> videoPrjPage = iVideoProjectService.getPrjMapsPage(page,ewVideoProjectPrj);
        String userUuid = "";
        String videoUrl = "";
        if(videoPrjPage.getRecords().size() != 0){
            userUuid = videoPrjPage.getRecords().get(0).get("userUuid").toString();
            videoUrl = videoPrjPage.getRecords().get(0).get("videoUrl").toString();
        }
        return "redirect:toVideoUrl.do?userUuid=" + userUuid + "&videoUrl=" + videoUrl;
    }


    /**
     * 视频监控实时数据详情
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("videoDevDataDesc.do")
    public String videoDevDataDesc(String id, HttpServletRequest request) {
        logger.info("视频监控实时数据详情-->>开始");
        Map<String , Object> videoDevData = iVideoProjectService.getPrjMapById(id);
        request.setAttribute("videoDevData", videoDevData);
        logger.info("视频监控实时数据详情-->>结束");
        return "vi/videoDevData/videoDevDataDesc";
    }

    /**
     * 获取视频监控设备列表
     * @param in
     */
    @RequiresPermissions("rules:vi_videoDevData_deviceListBtn")
    @RequestMapping(value = "/videoDeviceList.do")
    public void videoDeviceList(HttpServletResponse response, VideoProjectIn in) {
        try {
            logger.info("获取视频监控设备列表-->>开始");
            //设置条件
            WrapperUtil<VideoProject> ewVideoProject = new WrapperUtil<VideoProject>();
            ewVideoProject.setEntity(new VideoProject());
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewVideoProject.eq("prId",in.getPrId());
            }
            if(null != in.getOnLineStatus()){
                ewVideoProject.in("onLineStatus",String.valueOf(in.getOnLineStatus()));
            }
            ewVideoProject.eq("dataState",1);
            List<VideoProject> videoProjectList = iVideoProjectService.selectList(ewVideoProject);
            JSONArray array = new JSONArray();
            for(VideoProject videoProject : videoProjectList){
                JSONObject obj = new JSONObject(videoProject);
                //在线时间
                String onlineTime = "";
                Integer onlineState = videoProject.getOnLineStatus();
                if(onlineState == 1){
                    Date date = new Date();
                    Date dateOnline = new Date();
                    if(null != videoProject.getUpdateDate()){
                        dateOnline = videoProject.getUpdateDate();
                    }else{
                        dateOnline = videoProject.getCreateDate();
                    }
                    onlineTime = DateUtils.getDifferenceTime(dateOnline,date);
                }
                obj.put("onlineTime",onlineTime);
                array.put(obj);
            }
            ResManager.getJsonOut(response).print(array);
            logger.info("获取视频监控设备列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
